package com.example.mytk2_4;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.mytk2_4.R;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Button bt_con,bt_du;
    TextView tv_state;
    EditText et_ip,et_port,et_state,et_place,et_time;
    Socket socket;
    DataInputStream dataInputStream;
    String ip,port;
    byte buff[] = new byte[20];
    int time[] = new int[20];
    int wd,mb;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        InitView();
    }

    public void InitView()
    {
        bt_con = findViewById(R.id.bt_con);
        bt_du = findViewById(R.id.bu_du);
        tv_state = findViewById(R.id.tv_state);
        et_ip = findViewById(R.id.et_ip);
        et_place = findViewById(R.id.et_place);
        et_port = findViewById(R.id.et_port);
        et_state = findViewById(R.id.et_state);
        et_time = findViewById(R.id.et_time);

        bt_con.setOnClickListener(this);
        bt_du.setOnClickListener(this);
    }

    void startThread(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    socket = new Socket(ip, Integer.parseInt(port));
                    dataInputStream = new DataInputStream(socket.getInputStream());
                    tv_state.setText("已连接！");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                while(socket != null && socket.isConnected()){
                    try {
                        int read = dataInputStream.read(buff);
                        System.out.println("接收数据长度为："+ read);
                        if(read != 6 || buff[0] != (byte) 0x55 || buff[1] != (byte) 0xAA) continue;
                        if(buff[3] == (byte) 0x07){
//                            wd = Integer.parseInt(buff[5].substring(2, input.length()), 16);
                            wd = 0x0FF & buff[5];
                            if(buff[4] != (byte) 0x0){
                                wd = -1 * wd;
                            }
                        }
                        if(buff[3] == (byte) 0x15){

                            mb = 0x0FF & buff[5];
                            if(buff[4] != (byte) 0x0){
                                mb = -1 * mb;
                            }
                        }
//                        实现显示气温预警，其中35<气温<37 显示黄色，37<气温<39 显示橙色，39<气温显示红色
                            MainActivity.this.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
//                                    String a = String.valueOf(mb);
                                    et_state.setText(String.valueOf(mb));
                                    et_place.setText(String.valueOf(wd));
                                    if(wd < 37 & mb ==0){
                                        et_time.setText("温度正常!");
                                    }else if(wd > 37){
                                        et_time.setText("温度过高!");
                                    }
                                    if(mb > 60 & mb < 100){
                                        et_time.setText("脉搏正常！");
                                    }else if( (mb > 100 | mb < 60) & wd == 0){
                                        et_time.setText("脉搏过高或过低！");
                                    }
                                }
                            });


                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_con:
                ip = et_ip.getText().toString().trim();
                port = et_port.getText().toString().trim();
                startThread();
                break;
            case R.id.bu_du:
                try {
                    socket.close();
                    tv_state.setText("已断开！");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            default:
        }
    }
}